/**
 * UserController.java
 * 2016-9-22
 * 
 */
package com.zcy.controller;
import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;  
import org.springframework.ui.Model;  
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zcy.entity.User;
import com.zcy.service.UserService;

/**
 * @author 周成瑜
 *
 */
@Controller  
@RequestMapping("/user") 
public class UserController {
	 private static Logger log=LoggerFactory.getLogger(UserController.class);
	 
	 
	 /**
	  * 前台如果通过ajax向后台提交json数据
	  * 1前台：contentType : 'application/json;charset=UTF-8',
	  * 2：后台：这句话一定要与后台@requestBody相应，//如果没有它的话，只有@requestBody，那么我们的请求地址会是错误的。
	  */
	 
	 /**
	  * @PathVariable一般在restful风格中使用
	  */
	 @Resource  
     private UserService userService;  
	    // /user/test?id=1
	 
	    @RequestMapping(value="/test.action",method=RequestMethod.GET)  
	    public String test(HttpServletRequest request,Model model){  
	        int userId = Integer.parseInt(request.getParameter("id"));  
	        System.out.println("userId:"+userId);
	        User user=null;
	        if (userId==1) {
	             user = new User();  
	             user.setAge(11);
	             user.setId(1);
	             user.setPassword("123");
	             user.setUserName("javen");
	        }
	       
	        log.debug(user.toString());
	        model.addAttribute("user", user);  
	        return "index";  
	    }  
	    
	 // /user/showUser?id=1
	    @RequestMapping(value="/showUser.action",method=RequestMethod.GET)  
	    public String toIndex(HttpServletRequest request,Model model){  
	        int userId = Integer.parseInt(request.getParameter("id"));  
	        System.out.println("userId:"+userId);
	        User user = this.userService.getUserById(userId);
	        log.debug(user.toString());
	        model.addAttribute("user", user);  
	        return "showUser";  
	    }  
	    
	    // /user/showUser2?id=1
	    @RequestMapping(value="/showUser2.action",method=RequestMethod.GET)  
	    public String toIndex2(@RequestParam("id") String id,Model model){  
	        int userId = Integer.parseInt(id);  
	        System.out.println("userId:"+userId);
	        User user = this.userService.getUserById(userId);  
	        log.debug(user.toString());
	        model.addAttribute("user", user);  
	        return "showUser";  
	    }
	    
	 // /user/showUser3/{id}
	    @RequestMapping(value="/showUser3/{id}",method=RequestMethod.GET)  
	    public String toIndex3(@PathVariable("id")String id,Map<String, Object> model){  
	        int userId = Integer.parseInt(id);  
	        System.out.println("userId:"+userId);
	        User user = this.userService.getUserById(userId);  
	        log.debug(user.toString());
	        model.put("user", user);  
	        return "showUser";  
	    }  
	    
	 // /user/{id}
	    @RequestMapping(value="/{id}",method=RequestMethod.GET)  
	    public @ResponseBody User getUserInJson(@PathVariable String id,Map<String, Object> model){  
	        int userId = Integer.parseInt(id);  
	        System.out.println("userId:"+userId);
	        User user = this.userService.getUserById(userId);  
	        log.info(user.toString());
	        return user;  
	    }  
	    
	 // /jsontype/{id}
	    @RequestMapping(value="/jsontype/{id}",method=RequestMethod.GET)  
	    public ResponseEntity<User>  getUserInJson2(@PathVariable String id,Map<String, Object> model){  
	        int userId = Integer.parseInt(id);  
	        System.out.println("userId:"+userId);
	        User user = this.userService.getUserById(userId);  
	        log.info(user.toString());
	        return new ResponseEntity<User>(user,HttpStatus.OK);  
	    } 
	    
	    //文件上传、
	    @RequestMapping(value="/upload.action")
	    public String showUploadPage(){
	        return "file";
	    }
	    
	    @RequestMapping(value="/doUpload.action",method=RequestMethod.POST)
	    public String doUploadFile(@RequestParam("file")MultipartFile file) throws IOException{
	        if (!file.isEmpty()) {
	            log.info("Process file:{}",file.getOriginalFilename());
	        }
	        FileUtils.copyInputStreamToFile(file.getInputStream(), new File("E:\\",System.currentTimeMillis()+file.getOriginalFilename()));
	        return "succes";
	    }
}
